<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Camera Data Monitor</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .camera-card {
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
        }
        .completed {
            background-color: #e6ffe6;
        }
        .incomplete {
            background-color: #ffe6e6;
        }
        .progress-bar {
            height: 20px;
            background-color: #f0f0f0;
            border-radius: 10px;
            margin: 10px 0;
        }
        .progress {
            height: 100%;
            border-radius: 10px;
            background-color: #4CAF50;
        }
        a {
            color: #0066cc;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>Camera Data Status</h1>
    <div id="camera-container">
        <!-- 相机数据将通过JavaScript动态加载 -->
    </div>
    <script>
        function loadCameraData() {
            fetch('/api/data')
                .then(response => response.json())
                .then(data => {
                    const container = document.getElementById('camera-container');
                    container.innerHTML = '';
                    
                    // 计算总体平均完成率
                    let totalRate = 0;
                    let cameraCount = 0;
                    
                    for (const [cameraName, rate] of Object.entries(data.completion_rates)) {
                        totalRate += rate;
                        cameraCount++;
                        
                        const cameraCard = document.createElement('div');
                        cameraCard.className = `camera-card ${rate === 100 ? 'completed' : 'incomplete'}`;
                        
                        cameraCard.innerHTML = `
                            <h2>Camera ${cameraName}</h2>
                            <div class="progress-bar">
                                <div class="progress" style="width: ${rate}%"></div>
                            </div>
                            <p>Completion Rate: ${rate.toFixed(1)}%</p>
                            ${data.camera_data[cameraName]?.image_stats?.filter(img => img.incomplete_steps.length > 0).length > 0 ? 
                                `<details>
                                    <summary>Show incomplete images (${data.camera_data[cameraName].image_stats.filter(img => img.incomplete_steps.length > 0).length})</summary>
                                    <ul>
                                        ${data.camera_data[cameraName].image_stats
                                            .filter(img => img.incomplete_steps.length > 0)
                                            .map(img => `
                                                <li>
                                                    ${img.image_name} - 
                                                    Incomplete steps: ${img.incomplete_steps.join(', ')}
                                                    (${img.completion_rate.toFixed(1)}% completed)
                                                </li>`
                                            ).join('')}
                                    </ul>
                                </details>` : 
                                '<p>All images completed</p>'
                            }
                            <p>Last Updated: ${data.timestamp}</p>
                        `;
                        
                        container.appendChild(cameraCard);
                    }
                    
                    // 添加总体统计
                    if (cameraCount > 0) {
                        const avgRate = totalRate / cameraCount;
                        const summary = document.createElement('div');
                        summary.className = 'camera-card';
                        summary.innerHTML = `
                            <h2>Overall Statistics</h2>
                            <div class="progress-bar">
                                <div class="progress" style="width: ${avgRate}%"></div>
                            </div>
                            <p>Average Completion Rate: ${avgRate.toFixed(1)}%</p>
                            <p>Total Cameras: ${cameraCount}</p>
                        `;
                        container.prepend(summary);
                    }
                })
                .catch(error => console.error('Error loading camera data:', error));
        }
        
        function showImageDetails(cameraName, imageName) {
            const cameraData = camera_data[cameraName];
            if (!cameraData) return;
            
            const image = cameraData.image_stats.find(img => img.image_name === imageName);
            if (!image) return;
            
            const modal = document.createElement('div');
            modal.style.position = 'fixed';
            modal.style.top = '0';
            modal.style.left = '0';
            modal.style.width = '100%';
            modal.style.height = '100%';
            modal.style.backgroundColor = 'rgba(0,0,0,0.7)';
            modal.style.display = 'flex';
            modal.style.justifyContent = 'center';
            modal.style.alignItems = 'center';
            modal.style.zIndex = '1000';
            
            modal.innerHTML = `
                <div style="background: white; padding: 20px; border-radius: 5px; max-width: 80%; max-height: 80%; overflow: auto;">
                    <h2>Image Details: ${imageName}</h2>
                    <p>Completion Rate: ${image.completion_rate.toFixed(1)}%</p>
                    <p>Completed Steps: ${image.completed_steps.join(', ') || 'None'}</p>
                    <p>Incomplete Steps: ${image.incomplete_steps.join(', ') || 'None'}</p>
                    <button onclick="this.parentElement.parentElement.remove()">Close</button>
                </div>
            `;
            
            document.body.appendChild(modal);
        }
        
        // 初始加载数据
        loadCameraData();
        
        // 每分钟自动刷新数据
        setInterval(loadCameraData, 60000);
    </script>
</body>
</html>